package com.totoro.rabbitmq.java;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * abbitMQ纯java实现消息发送
 * Created by Niki on 2018/3/21 15:43
 */
public class RabbitMQSend {
    private static final String QUEUE_NAME = "HELLO";

    public static void send() throws IOException, TimeoutException {
        ConnectionFactory connectionfactory = new ConnectionFactory();
        connectionfactory.setHost("localhost");
        Connection connection = connectionfactory.newConnection();
        Channel channel = connection.createChannel();

        /// 随机创建一个队列，并且与创建的Connection绑定，且当最后一个consumer断开删除 （独占性 true，自动删除true，持久化false）
        channel.queueDeclare();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String msg = "Hello RabbitMQ !";
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        channel.close();
        connection.close();
    }
}
